package com.example.administrator.himalaya.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.administrator.himalaya.R;
import com.example.administrator.himalaya.adapter.AnchorAdapter;
import com.example.administrator.himalaya.constants.HttpConstant;
import com.example.administrator.himalaya.entity.anchor.AnchorArrayBeans;
import com.google.gson.Gson;
import com.rock.teachlibrary.http.HttpUtil;


public class FragmentDicsoverAnchor extends BasicFragment implements HttpUtil.RequestCallBack {
    public static final String TAG = FragmentDicsoverAnchor.class.getName();
    private ListView mListView;
    private AnchorAdapter adapter;
    private int pageNum = 1;
    private boolean isBottom;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        layout = inflater.inflate(R.layout.fragment_discover_anchor, container, false);
        return this.layout;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        initData();
    }

    private void initData() {
        HttpUtil.getStringAsync(HttpConstant.ANCHOR_URL + pageNum, this);
//        adapter.notifyDataSetChanged();
    }

    private void initView() {
        mListView = ((ListView) layout.findViewById(R.id.fragment_discover_anchor_listview));
        adapter = new AnchorAdapter(getActivity(), null);
        mListView.setAdapter(adapter);
//        mListView.setOnScrollListener(this);
//        initData(pageNum);
    }

    @Override
    public void onFailure() {

    }

    @Override
    public void onSuccess(String result, String url) {
        Gson gson = new Gson();
        AnchorArrayBeans anchorArrayBeans = gson.fromJson(result, AnchorArrayBeans.class);
        adapter.update(anchorArrayBeans.getList());
    }

//    @Override
//    public void onScrollStateChanged(AbsListView view, int scrollState) {
//        if (isBottom == true && scrollState == SCROLL_STATE_IDLE) {
//            pageNum++;
////            initData(pageNum);
////            HttpUtil.getStringAsync(HttpConstant.ANCHOR_URL + pageNum, this);
//        }
//    }
//
//    @Override
//    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//        if (firstVisibleItem + visibleItemCount == totalItemCount) {
//            isBottom = true;
//        } else {
//            isBottom = false;
//        }
//    }
}
